package com.pktechan.api.sendMiniMessage;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.type.TypeReference;
import com.pktechan.api.dto.MiniSubscribeMessage;
import com.pktechan.api.sendMiniMessage.enumration.SendMiniMessageEnum;
import com.pktechan.api.utils.Config;
import com.pktechan.api.utils.HttpUtil;
import com.pktechan.api.utils.ResponseMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: 发送微信消息基类
 * @author: qx.zhang
 * @create: 2019-10-15 17:12
 **/
public class BaseSendMiniMessage {
    private static final Logger logger = LoggerFactory.getLogger(BaseSendMiniMessage.class);

    /**
     * 获取发送消息行为对象
     *
     * @param sendMiniMessageEnum
     * @return
     */
    public static SendMiniMessageStrategy getSendMiniMessage(SendMiniMessageEnum sendMiniMessageEnum) {
        Map<SendMiniMessageEnum, SendMiniMessageStrategy> map = SendMiniMessageSingleton.getInstance();
        SendMiniMessageStrategy sendMiniMessageStrategy = map.get(sendMiniMessageEnum);
        return sendMiniMessageStrategy;
    }

    /**
     * 发送微信消息
     */
    public String sendSubscribeMessage(MiniSubscribeMessage miniSubscribeMessage) {
        String httpUrl = Config.readConfig("wechat.httpUrl") + Config.readConfig("wechat.sendSubscribeMessage");
        HashMap<String, Object> params = new HashMap();
        params.put("subscribeMessageJson", JSON.toJSONString(miniSubscribeMessage));
        logger.info("subscribeMessageJson------" + params.get("subscribeMessageJson"));
        try {
            ResponseMessage<String> rs = HttpUtil.doPostForResponseMessage(httpUrl, params, new TypeReference<ResponseMessage<String>>() {
            });
            logger.info("result：" + rs.isSuccess());
            if (rs.isSuccess()) {
                logger.debug(rs.getData());
            } else {
                logger.debug(rs.getErrorMessage());
            }
            return rs.getData();
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug("系统异常");
        }
        return null;
    }

}
